#include "servicecode/SplitTool.h"

//使用俄汉式，程序启动就创建
SplitTool* SplitTool::_pInstance = new SplitTool();

SplitTool *SplitTool::getInstance()
{
    if(nullptr == _pInstance){
        _pInstance = new SplitTool();
    }
    return _pInstance;
}


SplitTool::SplitTool()
    : _jieba("data/dict/jieba.dict.utf8",
             "data/dict/hmm_model.utf8",
             "data/dict/user.dict.utf8",
             "data/dict/idf.utf8",
             "data/dict/stop_words.utf8")
{
    LogInfo("生成结巴工具，加载停止词");
    // 加载中文停止词
    ifstream ifs_ch_stop("data/yuliao/stop_words_zh.txt");
    if (!ifs_ch_stop.is_open())
    {
        LogError("stop_words_eng.txt open failed");
    }
    string ch_word;
    while (ifs_ch_stop >> ch_word)
    {
        _stop_ch.insert(ch_word);
    }
}

void SplitTool::Splitwords(const string & str,vector<string>& words)
{   
    vector<string> buf;
    _jieba.Cut(str,buf,true);
    for(auto word : buf){
        if(_stop_ch.find(word) == _stop_ch.end()){
            words.push_back(word);
        }
    }
}

void SplitTool::Splitwords(const string & str,map<string,int> & words)
{
    vector<string> buf;
    _jieba.Cut(str,buf,true);
    for(auto word : buf){
        if(_stop_ch.find(word) == _stop_ch.end()){
            ++words[word];
        }
    }
}
